<#-- Includes -->
<#include "propertyutils.ftl">
<#include "viewutils.ftl">

<#-- Renders text input for given property -->
<#macro renderText componentName propName nestedPropName="" id="" formatType="" formatPattern="">
  <stripes:text<#if id != ""> id="${id}"</#if><#if formatType != ""> formatType="${formatType}"</#if><#if formatPattern != ""> formatPattern="${formatPattern}"</#if> name="<#if componentName != "">${componentName}.</#if>${propName}<#if nestedPropName != "">.${nestedPropName}</#if>"<#if formatType = "entity"> disabled="disabled"</#if> />
</#macro>

<#-- Renders text area for given property -->
<#macro renderTextArea length componentName propName nestedPropName="">
  <stripes:textarea name="${componentName}.${propName}<#if nestedPropName != "">.${nestedPropName}</#if>" rows="<#if length gt 1024>10<#else>5</#if>" />
</#macro>

<#-- Renders input for string (will automatically decide whether use text or text area) -->
<#macro renderString length componentName propName nestedPropName="">
<#if length gt 256>
  <@renderTextArea length=length componentName=componentName propName=propName nestedPropName=nestedPropName />
<#else>
  <@renderText componentName=componentName propName=propName nestedPropName=nestedPropName />
</#if>
</#macro>

<#-- Renders check box for given property -->
<#macro renderCheckBox componentName propName nestedPropName="">
  <stripes:checkbox name="${componentName}.${propName}<#if nestedPropName != "">.${nestedPropName}</#if>" />
</#macro>

<#-- Renders hidden property -->
<#macro renderHidden componentName propName nestedPropName="" id="">
  <stripes:hidden name="<#if componentName != "">${componentName}.</#if>${propName}<#if nestedPropName != "">.${nestedPropName}</#if>" id="<#if id !="">${id}</#if>" />
</#macro>

<#-- Renders date picker for given property -->
<#macro renderDatePicker componentName propName nestedPropName="">
<#assign format = "yy-mm-dd" />
<#assign idstr = "picker-"+propName/>
<#if nestedPropName != "">
<#assign idstr = "picker-"+nestedPropName/>
</#if>
  <script type="text/javascript">
    $(function() {
      $("#${idstr}").datepicker({dateFormat: "${format}", showOn: 'button'});
    });
  </script>
  <@renderText componentName=componentName propName=propName nestedPropName=nestedPropName id=idstr formatType="date" formatPattern=format />
</#macro>

<#-- Renders datetime picker for given property -->
<#macro renderDateTimePicker componentName propName nestedPropName="">
<#assign format = "yy-mm-dd" />
<#assign idstr = "picker-"+propName/>
<#if nestedPropName != "">
<#assign idstr = "picker-"+nestedPropName/>
</#if>
  <script type="text/javascript">
    $(function() {
      $('#${idstr}').datepicker({dateFormat: "${format}", duration: '', showOn: 'button', showTime: true, time24h: true, constrainInput: false});
    });
  </script>
  <@renderText componentName=componentName propName=propName nestedPropName=nestedPropName id=idstr formatType="datetime" formatPattern=format+" HH:mm" />
</#macro>

<#-- Renders entity picker for given property, if ViewEntityPicker is present for Entity, use it -->
<#macro renderEntityPicker prop componentName propName nestedPropName="">
<#assign idstr = "picker-"+propName/>
<#assign entName = prop.type.name?split(".")?last/>
<#if nestedPropName != "">
<#assign idstr = "picker-"+nestedPropName/>
<#assign entName = nestedPropName/>
</#if>
  <script type="text/javascript">
    $(function() {
      $('#${idstr}').entitypicker({showOn: 'button', entityName: "${getPickerName(entName)?uncap_first}", hiddenInputId: "${idstr}Hidden", labelText: ""});
    });
  </script>
  <@renderText componentName="" propName=propName+"PickerDisplayName" nestedPropName=nestedPropName id=idstr formatType="entity" />
  <@renderHidden componentName="" propName=propName+"Key" nestedPropName=nestedPropName id="${idstr}Hidden" />
</#macro>

<#-- Renders id property -->
<#macro renderId componentName propName nestedPropName="">
  <@renderHidden componentName=componentName propName=propName nestedPropName=nestedPropName />
  <c:choose>
    <c:when test="${'$'}{(empty actionBean) || (empty actionBean.${componentName}.${propName}<#if nestedPropName != "">.${nestedPropName}</#if>) }">
      <fmt:message bundle="${'$'}{msgs}" key="custom.global.label.willBeGenerated"/>
    </c:when>
    <c:otherwise>
      ${'$'}{actionBean.${componentName}.${propName}<#if nestedPropName != "">.${nestedPropName}</#if>}
    </c:otherwise>
  </c:choose>
</#macro>

<#-- Renders property -->
<#macro renderProperty componentName prop nestedProp=0 hasNested=false>
<#assign tmp = prop />
<#assign nestedPropName = "" />
<#if hasNested>
<#assign tmp = nestedProp/>
<#assign nestedPropName = nestedProp.name />
</#if>
<#if isToOne(tmp)>
  <@renderEntityPicker prop=prop componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isId(tmp)>
  <@renderId componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isDate(tmp)>
  <@renderDatePicker componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isTime(tmp)>
  <@renderDateTimePicker componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isTimestamp(tmp)>
  <@renderDateTimePicker componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isBigDecimal(tmp)>
  <@renderText componentName=componentName propName=prop.name nestedPropName=nestedPropName formatType="number"/>
<#elseif isBigInteger(tmp)>
  <@renderText componentName=componentName propName=prop.name nestedPropName=nestedPropName formatType="number"/>
<#elseif isInteger(tmp)>
  <@renderText componentName=componentName propName=prop.name nestedPropName=nestedPropName formatType="number"/>
<#elseif isLong(tmp)>
  <@renderText componentName=componentName propName=prop.name nestedPropName=nestedPropName formatType="number"/>
<#elseif isFloat(tmp)>
  <@renderText componentName=componentName propName=prop.name nestedPropName=nestedPropName formatType="number"/>
<#elseif isDouble(tmp)>
  <@renderText componentName=componentName propName=prop.name nestedPropName=nestedPropName formatType="number"/>
<#elseif isBoolean(tmp)>
  <@renderCheckBox componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isString(tmp)>
  <@renderString length=length(tmp) componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
</#if>
</#macro>

<#-- Renders property input with layout -->
<#macro renderPropertyLayout componentName prop nestedProp=0 hasNested=false>
  <div class="prop">
    <span class="propname">
      <stripes:label for="${componentName}.${prop.name}<#if hasNested>.${nestedProp.name}</#if>"/>:
    </span>
    <span class="propvalue">
      <@renderProperty componentName=componentName prop=prop nestedProp=nestedProp hasNested=hasNested/>
    </span>
  </div>
</#macro>

<#-- Renders input for all properties -->
<#macro renderProperties>
<#foreach prop in pojo.allPropertiesIterator>
<#if !c2h.isCollection(prop) && prop != pojo.versionProperty!>
<#if pojo.isComponent(prop)>
<#foreach nestedProp in prop.value.propertyIterator>
  <@renderPropertyLayout componentName=pojo.shortName?uncap_first prop=prop nestedProp=nestedProp hasNested=true/>
</#foreach>
<#else>
  <@renderPropertyLayout componentName=componentName prop=prop/>
</#if>
</#if>
</#foreach>
</#macro>

<#-- Renders search for date -->
<#macro renderDateSearch componentName propName nestedPropName="">
<#assign tmpName=propName/>
<#if nestedPropName != "">
<#assign tmpName = nestedPropName/>
</#if>
  <@renderDatePicker componentName=componentName propName=tmpName+"From" />
  <c:out value=" - " />
  <@renderDatePicker componentName=componentName propName=tmpName+"To" />
</#macro>

<#-- Renders search for property -->
<#macro renderPropertySearch componentName prop nestedProp=0 hasNested=false>
<#assign tmp = prop />
<#assign nestedPropName = "" />
<#if hasNested>
<#assign tmp = nestedProp/>
<#assign nestedPropName = nestedProp.name />
</#if>
<#if isDate(tmp)>
  <@renderDateSearch componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isTime(tmp)>
  <@renderDateSearch componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isTimestamp(tmp)>
  <@renderDateSearch componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
<#elseif isString(tmp)>
  <@renderText componentName=componentName propName=prop.name nestedPropName=nestedPropName/>
</#if>
</#macro>

<#-- Renders property search with layout -->
<#macro renderPropertySearchLayout componentName prop nestedProp=0 hasNested=false>
  <div class="prop">
    <span class="propname">
      <stripes:label for="${componentName}.${prop.name}<#if hasNested>.${nestedProp.name}</#if>"/>:
    </span>
    <span class="propvalue">
      <@renderPropertySearch componentName=componentName prop=prop nestedProp=nestedProp hasNested=hasNested/>
    </span>
  </div>
</#macro>

<#-- Renders search for all string/datetime properties -->
<#macro renderSearchProperties>
<#foreach prop in pojo.allPropertiesIterator>
<#if !c2h.isCollection(prop) && !isToOne(prop) && prop != pojo.versionProperty!>
<#if pojo.isComponent(prop)>
<#foreach nestedProp in prop.value.propertyIterator>
<#if isString(nestedProp) || isDate(nestedProp) || isTimestamp(nestedProp) || isTime(nestedProp)>
  <@renderPropertySearchLayout componentName=pojo.shortName?uncap_first prop=prop nestedProp=nestedProp hasNested=true/>
</#if>
</#foreach>
<#else>
<#if isString(prop) || isDate(prop) || isTimestamp(prop) || isTime(prop)>
  <@renderPropertySearchLayout componentName=componentName prop=prop/>
</#if>
</#if>
</#if>
</#foreach>
</#macro>

<#-- Renders string filter cell -->
<#macro renderStringFilterCell controllerPackage entityName prop nestedProp=0 hasNested=false>
<#assign tmp = prop />
<#assign nestedPropName = "" />
<#if hasNested>
<#assign tmp = nestedProp/>
<#assign nestedPropName = nestedProp.name />
</#if>
  <td>
<#if isString(tmp)>
    <@renderText componentName=pojo.shortName?uncap_first propName=prop.name nestedPropName=nestedPropName/>
</#if>
  </td>
</#macro>

<#-- Renders filter columns for strings -->
<#macro renderStringFilterCells controllerPackage entityName picker=false>
<#assign componentName = entityName?uncap_first/>
<#assign propertyList = createPropertyList()/>
<#foreach prop in propertyList>
<#if pojo.isComponent(prop)>
<#foreach nestedProp in prop.value.propertyIterator>
<#if !picker || (!(nestedProp.name?matches("id.*")) && !(nestedProp.name?matches("[pP]icker[dD]isplay"))) >
  <@renderStringFilterCell controllerPackage=controllerPackage entityName=entityName prop=prop nestedProp=nestedProp hasNested=true/>
</#if>
</#foreach>
<#else>
  <@renderStringFilterCell controllerPackage=controllerPackage entityName=entityName prop=prop/>
</#if>
</#foreach>
</#macro>

<#-- Renders picker link -->
<#--<#macro renderPickerLink entityName index>
<#if hasPickerView(entityName)>
  <a href="javascript:select('${entityName?uncap_first}','${'$'}{index.${pojo.identifierProperty.name}}')">
    <fmt:message bundle="${'$'}{msgs}" key="custom.global.label.select"/>
  </a>
<#else>
  <a href="javascript:select('${entityName?uncap_first}','${'$'}{index.${pojo.identifierProperty.name}}')">
    <fmt:message bundle="${'$'}{msgs}" key="custom.global.label.select"/>
  </a>
</#if>
</#macro>-->
